

import {getActDetail,applyCancel,applyAct} from "../../../api/act";

import Dialog from '@vant/weapp/dialog/dialog';

const globalData = getApp().globalData
const { config,colorOption } = globalData;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    colorOption,
    showData: {},
    contentId:null,
    // 显示加载中
    isLoading: true,
    actId:null,
    openConfirm:false,
    phone:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {id} = options;
    if (id) {
      this.setData({actId:id,phone:globalData.userInfo.phone})
      this.getData(id);
    }
  },

  // 获取数据
  getData(id) {
    wx.showLoading({title: '加载中',})
    getActDetail({
      data: {
        id:id,
      },
      callBack: (res) => {
        let resData = res.data;
        resData.image = resData.image ? config.localImgSrc + resData.image: '';
        if (resData.participants && resData.participants.length){
          resData.participants.forEach(item=>{
            item.attrDataMap.TX = item.attrDataMap.TX ? config.localImgSrc + item.attrDataMap.TX : (item.attrDataMap.XB == 1 ? '/images/mine/defaultAvatar_male.png' : '/images/mine/defaultAvatar_female.png');
          })
          resData.participantLength = resData.participants.length
          resData.participantList = resData.participants.splice(0,10)
        }else {
          resData.participantLength = 0;
          resData.participantList = [];
        }
        this.setData({
          showData:resData,
          isLoading: false,
        })
        wx.hideLoading();
      },
    })
  },
  // 预览图片
  previewImage(e) {
    const {index,list} = e.currentTarget.dataset;
    wx.previewImage({
      current: list[index],
      urls: list,
    });
  },
  handleFocus(){
    if (this.data.showData.isApply){
      Dialog.confirm({
        title: '是否确认取消报名此活动',
      })
        .then(() => {
          applyCancel({
            data: {
              activityId:this.data.actId,
            },
            callBack: (res) => {
              wx.showToast({
                title: '取消成功',
                icon: 'success',
                duration: 2000
              })
              this.getData(this.data.actId)
            },
          })
        })
        .catch(() => {
          // on cancel
        });
    }else {
      this.setData({openConfirm:true})
    }
  },
  onCloseConfirm(){
    this.setData({openConfirm:false})
  },
  confirmFocus(){
    applyAct({
      data: {
        activityId:this.data.actId,
        contactNumber:this.data.phone
      },
      callBack: (res) => {
        wx.showToast({
          title: '报名成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      },
    })
  },
  goBack(){
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData(this.data.actId);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
